<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
      <div v-if="dialogObj.isShow">
        <c-dialog
          class="dialog-box"
          :is-show="dialogObj.isShow"
          :title="dialogObj.title"
          close-btn
          @before-close="dialogObj.isShow = false"
          @on-submit="dialogConfirm"
        >
          <audit-refund ref="childRef" :init-data.sync="dialogObj.initData"></audit-refund>
        </c-dialog>
      </div>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import dictObj from '@/store/dictData'
import utils from 'utils'
import CDialog from 'components/dialog'
import AuditRefund from './audit'

export default {
  name: 'refund',
  mixins: [mixinTable],
  components: {
    CDialog,
    AuditRefund
  },
  data(vm) {
    return {
      dialogObj: {},
      tableInnerBtns: [
        {
          name: '审核',
          icon: 'el-icon-view',
          width: 150,
          notBtn: row => row.status === '2' || row.status === '3' || row.status === '4' || row.status === '5' || row.status === '6',
          handle(row) {
            const { id, name, money } = row
            vm.showDialog({
              initData: {
                ids: `[${id}]`,
                name,
                money: utils.formatMoney(money),
                check: 1
              }
            })
          }
        },
        {
          name: '取消退款',
          icon: 'el-icon-close',
          notBtn: row => row.status === '2' || row.status === '3' || row.status === '4' || row.status === '5' || row.status === '6',
          handle(row) {
            vm.confirmTip('确定取消退款', () => {
              vm.confirmRefund(row.id)
            })
          }
        }
      ],
      // 表格内操作按钮
      tableHeader: [
        {
          label: '所属区域',
          prop: 'cityName'
        },
        {
          label: '退款单号',
          prop: 'refundOrderId'
          // search: {
          //   type: 'input'
          // }
        },
        {
          label: '退款金额',
          prop: 'money',
          formatter: row => row && utils.formatMoney(row.money)
        },
        {
          label: '退款申请人',
          prop: 'name',
          search: {
            type: 'input'
          }
        },
        {
          label: '退款手机号',
          prop: 'refundCustomerPhone'
        },
        {
          label: '退款类型',
          prop: 'refundType',
          formatter: row => (row && row.refundType === 'D' ? '押金' : '余额')
        },
        {
          label: '账户类型',
          prop: 'repayType',
          formatter: row => row && vm.setTableColumnLabel(row.repayType, 'repayTypeList')
        },
        {
          label: '退款状态',
          prop: 'status',
          formatter: row => row && vm.setTableColumnLabel(row.status, 'refundStatusList'),
          search: {
            type: 'dict',
            optionsList: dictObj.refundStatusList
          }
        },
        {
          label: '创建时间',
          prop: 'createTime',
          width: 160
        },
        {
          label: '结束时间',
          prop: 'finallyHandleTime',
          width: 160
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime)
      this.isLoading = true
      this.$api.member
        .fetch('customerRefundLogList', {
          ...searchDate,
          ...other,
          rows: pageNo,
          offset: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    /**
     * dialog对话框数据处理
     * @opts {*}
     */
    showDialog(opts) {
      this.dialogObj = {
        isShow: true,
        title: opts.title || '客服退款审核',
        initData: opts.initData
      }
    },
    /**
     * dialog新增、编辑处理函数
     */
    dialogConfirm() {
      const childRef = this.$refs.childRef
      childRef.$refs.formRef.validate(valid => {
        if (valid) {
          const { check, ids, remark } = childRef.formModel
          if (check === 2 && !remark) {
            return this.$msgTip('请填写不通过原因', 'warning')
          }
          this.$api.member
            .fetch('auditCustomerRefund', {
              ids,
              check,
              remark
            })
            .then(() => {
              this.responeHandle('审核成功')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消退款
    confirmRefund(id) {
      this.$api.member
        .fetch('cancelRefund', {
          id
        })
        .then(() => {
          this.responeHandle('取消成功')
        })
    }
  }
}
</script>
